package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author durant
 * @create 2021-09-13 15:01
 */
@Controller
@Slf4j
@ResponseBody
public class PaymentController {
    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverport;

    @PostMapping(value = "/payment/create")
    public CommonResult<Payment> create(@RequestBody Payment payment) {

        int result = paymentService.create(payment);
        log.info("*******插入结果" + result+"haha ");
        int age=10/2;
        if (result > 0) {
//            成功
            return new CommonResult(200, "插入数据库成功,serverPort:"+serverport, result);
        } else {
            return new CommonResult(444, "插入数据库失败,serverPort:"+serverport, null);
        }

    }

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult create(@PathVariable("id") long id) {

        Payment payment = paymentService.getPaymentById(id);
        log.info("*******查到结果" + payment);

        if (payment !=null) {
//            成功
            return new CommonResult(200, "查询数据成功,serverPort:"+serverport, payment);
        } else {
            return new CommonResult(444, "查询数据失败,serverPort:"+serverport, null);
        }

    }
    @GetMapping(value = "/payment/feign/timeout")
    public String paymentFeignTimeout(){
        try { TimeUnit.SECONDS.sleep(3); }catch (Exception e) {e.printStackTrace();}
        return serverport;
    }
}
